import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';


// 创建key值实例
GlobalKey<ChildContext> childKey = GlobalKey();

class Children extends StatefulWidget {

  final String parentMessage;

  const Children({super.key,  required this.parentMessage});

  @override
  ChildContext createState() => ChildContext();

}

class ChildContext extends State<Children> {

  int content = 1;
  String _scanCode = '';
  String _childParams = '';


  void addContent() {
    setState(() {
      content = content + 1;
    });
  }

  void _toScan() async {
    final String? scanCode = await context.push('/scan');
    setState(() {
      _scanCode = scanCode ?? '';
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.amber,
      child: Column(
        children: [
          Row(
            children: [
              Text('父组件消息：${widget.parentMessage}')
            ],
          ),
          SizedBox(
            width: double.infinity,
            height: 100.0,
            child: Text('子组件的状态: $content'),
          ),
          ElevatedButton(
            onPressed: () {
              context.push('/about');
            },
            child: const Text('跳转')
          ),

          Text('扫码结果 $_scanCode'),
          ElevatedButton(onPressed: _toScan, child: const Text('跳转扫码')),
          const SizedBox(height: 20),

          Text('子路由参数 $_childParams'),
          ElevatedButton(onPressed: () async {
            final String? str = await context.push('/position');
            setState(() {
              _childParams = str ?? '';
            });
          }, child: const Text('调整到定位页面'))
        ],
      )
    );
  }

}